package com.mywang.weitravel.modules.redlocate;

import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.model.RedLocateListBean;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.net.VObserver;

import java.util.HashMap;
import java.util.List;

public class RedLocatePresenter extends BasePresenter<RedLocateContract.View> implements RedLocateContract.Presenter {

    public RedLocatePresenter(RedLocateContract.View view) {
        super(view);
    }

    @Override
    public void requestData(String id) {
        mView.showLoading();
        HashMap<String, String> params = new HashMap<>();
        params.put("cate_id", id);
        RetrofitHelper.getInstance().getServer()
                .getRedLocateList(params)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<List<RedLocateListBean>>() {
                    @Override
                    protected void onSuccess(List<RedLocateListBean> listBeans) {
                        mView.onSuccess(listBeans);
                        mView.hideLoading();
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void markRed(int position, String id) {
        mView.showLoading();
        RetrofitHelper.getInstance().getServer()
                .likeRed(id)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<Object>() {
                    @Override
                    protected void onSuccess(Object o) {
                        mView.markRedSuccess(position);
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.hideLoading();
                    }
                });
    }
}
